package com.mo.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @ClassName KeyResolverConfiguration
 * @Description TODO
 * @Author Administrator
 * @Date 2021/8/24
 * @Version 1.0
 */
//@Configuration
public class KeyResolverConfiguration {
    /**
     * 基于请求路径:
     */
    //@Bean
    public KeyResolver pathKeyResolver(){
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                return Mono.just(exchange.getRequest().getPath().toString());
            }
        };
    }

    /**
     * 基于请求参数:
     */
    //@Bean
    public KeyResolver userKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getQueryParams()
        .getFirst("userId"));
    }

    /**
     * 基于ip:
     */
    //@Bean
    public KeyResolver ipKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getHeaders()
                .getFirst("x-Forwarded-For"));
    }
}
